#!/bin/bash

#set gpu
adb shell "echo 1 > /sys/class/kgsl/kgsl-3d0/force_clk_on"
adb shell "echo 10000000 > /sys/class/kgsl/kgsl-3d0/idle_timer"
adb shell "echo performance > /sys/class/kgsl/kgsl-3d0/devfreq/governor"
adb shell "echo 710000000 > /sys/class/kgsl/kgsl-3d0/gpuclk"

#set cpu work mode
adb shell "echo performance > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor"
adb shell "echo performance > /sys/devices/system/cpu/cpu1/cpufreq/scaling_governor"
adb shell "echo performance > /sys/devices/system/cpu/cpu2/cpufreq/scaling_governor"
adb shell "echo performance > /sys/devices/system/cpu/cpu3/cpufreq/scaling_governor"
adb shell "echo performance > /sys/devices/system/cpu/cpu4/cpufreq/scaling_governor"
adb shell "echo performance > /sys/devices/system/cpu/cpu5/cpufreq/scaling_governor"
adb shell "echo performance > /sys/devices/system/cpu/cpu6/cpufreq/scaling_governor"
adb shell "echo performance > /sys/devices/system/cpu/cpu7/cpufreq/scaling_governor"

#turn on/off cpu,0:off, 1:on
adb shell "echo 1 > /sys/devices/system/cpu/cpu0/online"
adb shell "echo 1 > /sys/devices/system/cpu/cpu1/online"
adb shell "echo 1 > /sys/devices/system/cpu/cpu2/online"
adb shell "echo 1 > /sys/devices/system/cpu/cpu3/online"
adb shell "echo 1 > /sys/devices/system/cpu/cpu4/online"
adb shell "echo 1 > /sys/devices/system/cpu/cpu5/online"
adb shell "echo 1 > /sys/devices/system/cpu/cpu6/online"
adb shell "echo 1 > /sys/devices/system/cpu/cpu7/online"
#modify cpu freq
adb shell "echo 1900800 >/sys/devices/system/cpu/cpu0/cpufreq/scaling_setspeed"
adb shell "echo 1900800 >/sys/devices/system/cpu/cpu1/cpufreq/scaling_setspeed"
adb shell "echo 1900800 >/sys/devices/system/cpu/cpu2/cpufreq/scaling_setspeed"
adb shell "echo 1900800 >/sys/devices/system/cpu/cpu3/cpufreq/scaling_setspeed"
adb shell "echo 1900800 >/sys/devices/system/cpu/cpu4/cpufreq/scaling_setspeed"
adb shell "echo 1900800 >/sys/devices/system/cpu/cpu5/cpufreq/scaling_setspeed"
adb shell "echo 1900800 >/sys/devices/system/cpu/cpu6/cpufreq/scaling_setspeed"
adb shell "echo 1900800 >/sys/devices/system/cpu/cpu7/cpufreq/scaling_setspeed"

current_time=`adb shell date "+%y_%m_%d_%H_%M_%S"`
mkdir $current_time

echo "-----------start grap systrace info--------------"
category="gfx view sched freq idle load"
python systrace.py -o ./$current_time/systrace_$current_time.html --time=30 --buf-size=20480 --app="com.htc.wavevr.platform_perf_certif_tool" --serial="f1b7e936" $category &
echo "-----------start grap gpu cpu info--------------------"
#exec 1>./$current_time/system_info_$current_time.csv

#echo "time,gpumaxfreq,gpuminfreq,gpucurfreq,gpuload,cpu0maxfreq,cup0minfreq,cpu0curfreq,cpu1maxfreq,cpu1minfreq,cpu1curfreq,cpu2maxfreq,cpu2minfreq,cpu2curfreq,cpu3maxfreq,cpu3minfreq,cpu3curfreq,cpu4maxfreq,cpu4minfreq,cpu4curfreq,cpu5maxfreq,cpu5minfreq,cpu5curfreq,cpu6maxfreq,cpu6minfreq,cpu6curfreq,cpu7maxfreq,cpu7minfreq,cpu7curfreq,gputemp,cpu0temp,cpu1temp,cpu2temp,cpu3temp,fps,battery_capacity,user_cpu_usage,system_cpu_usage,total_cpu_usage"
echo "nowtime,gpucurfreq,gpu_load,cpu0curfreq,cpu1curfreq,cpu2curfreq,cpu3curfreq,cpu4curfreq,cpu5curfreq,cpu6curfreq,cpu7curfreq,fps,battery_capacity,user_cpu_usage,system_cpu_usage,total_cpu_usage" &> ./$current_time/system_info_$current_time.csv
isToRunning=true
trap "isToRunning=false" SIGINT
while $isToRunning
do 
	nowtime=`adb shell date "+%y_%m_%d_%H_%M_%S"`

	#gpumaxfreq=`adb shell cat /sys/class/kgsl/kgsl-3d0/devfreq/max_freq`
	#gpuminfreq=`adb shell cat /sys/class/kgsl/kgsl-3d0/devfreq/min_freq`
	gpucurfreq=`adb shell cat /sys/class/kgsl/kgsl-3d0/devfreq/cur_freq` 
	
	#cpu0maxfreq=`adb shell cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq`
	#cpu0minfreq=`adb shell cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq`
	cpu0curfreq=`adb shell cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq`
	
	#cpu1maxfreq=`adb shell cat /sys/devices/system/cpu/cpu1/cpufreq/scaling_max_freq`
	#cpu1minfreq=`adb shell cat /sys/devices/system/cpu/cpu1/cpufreq/scaling_min_freq`
	cpu1curfreq=`adb shell cat /sys/devices/system/cpu/cpu1/cpufreq/scaling_cur_freq`

	#cpu2maxfreq=`adb shell cat /sys/devices/system/cpu/cpu2/cpufreq/scaling_max_freq`
	#cpu2minfreq=`adb shell cat /sys/devices/system/cpu/cpu3/cpufreq/scaling_min_freq`
	cpu2curfreq=`adb shell cat /sys/devices/system/cpu/cpu2/cpufreq/scaling_cur_freq`
	
	#cpu3maxfreq=`adb shell cat /sys/devices/system/cpu/cpu3/cpufreq/scaling_max_freq`
	#cpu3minfreq=`adb shell cat /sys/devices/system/cpu/cpu3/cpufreq/scaling_min_freq`
	cpu3curfreq=`adb shell cat /sys/devices/system/cpu/cpu3/cpufreq/scaling_cur_freq`

	#cpu4maxfreq=`adb shell cat /sys/devices/system/cpu/cpu4/cpufreq/scaling_max_freq`
	#cpu4minfreq=`adb shell cat /sys/devices/system/cpu/cpu4/cpufreq/scaling_min_freq`
	cpu4curfreq=`adb shell cat /sys/devices/system/cpu/cpu4/cpufreq/scaling_cur_freq`

	#cpu5maxfreq=`adb shell cat /sys/devices/system/cpu/cpu5/cpufreq/scaling_max_freq`
	#cpu5minfreq=`adb shell cat /sys/devices/system/cpu/cpu5/cpufreq/scaling_min_freq`
	cpu5curfreq=`adb shell cat /sys/devices/system/cpu/cpu5/cpufreq/scaling_cur_freq`
	
	#cpu6maxfreq=`adb shell cat /sys/devices/system/cpu/cpu6/cpufreq/scaling_max_freq`
	#cpu6minfreq=`adb shell cat /sys/devices/system/cpu/cpu6/cpufreq/scaling_min_freq`
	cpu6curfreq=`adb shell cat /sys/devices/system/cpu/cpu6/cpufreq/scaling_cur_freq`
	
	#cpu7maxfreq=`adb shell cat /sys/devices/system/cpu/cpu7/cpufreq/scaling_max_freq`
	#cpu7minfreq=`adb shell cat /sys/devices/system/cpu/cpu7/cpufreq/scaling_min_freq`
	cpu7curfreq=`adb shell cat /sys/devices/system/cpu/cpu7/cpufreq/scaling_cur_freq`

	#gputemp=`adb shell cat /sys/class/thermal/thermal_zone16/temp`
	#cpu0temp=`adb shell cat /sys/class/thermal/thermal_zone5/temp`
	#cpu1temp=`adb shell cat /sys/class/thermal/thermal_zone7/temp`
	#cpu2temp=`adb shell cat /sys/class/thermal/thermal_zone10/temp`
	#cpu3temp=`adb shell cat /sys/class/thermal/thermal_zone12/temp`
    
	fpsStr=$(adb logcat -d | grep "svr     : FPS: ")
    fps=${fpsStr##*: FPS: }
	
	gpu_load=`adb shell cat /sys/class/kgsl/kgsl-3d0/gpu_busy_percentage`
	
	process_str=`adb shell top -n 1 -m 1`
	
	#platform_perf_certif_tool_cpu_usage=`echo $process_str | gawk '/.*(com\.htc\.wavevr\.platform_perf_certif_tool)$/{print $5;}'`
	user_cpu_usage=`echo $process_str | gawk '/^(User.*,)/{printf("%d%",int($2));}'`
	system_cpu_usage=`echo $process_str | gawk '/^(User.*,)/{printf("%d%",int($4));}'`
	total_cpu_usage=`echo $process_str | gawk '/^(User.*,)/{printf("%d%",int($2)+int($4));}'`

    capacity=`adb shell cat /sys/class/power_supply/battery/capacity`

    #echo "$nowtime,$gpumaxfreq,$gpuminfreq,$gpucurfreq,$gpu_load,$cpu0maxfreq,$cpu0minfreq,$cpu0curfreq,$cpu1maxfreq,$cpu1minfreq,$cpu1curfreq,$cpu2maxfreq,$cpu2minfreq,$cpu2curfreq,$cpu3maxfreq,$cpu3minfreq,$cpu3curfreq,$cpu4maxfreq,$cpu4minfreq,$cpu4curfreq,$cpu5maxfreq,$cpu5minfreq,$cpu5curfreq,$cpu6maxfreq,$cpu6minfreq,$cpu6curfreq,$cpu7maxfreq,$cpu7minfreq,$cpu7curfreq,$gputemp,$cpu0temp,$cpu1temp,$cpu2temp,$cpu3temp,$fps,$capacity,$user_cpu_usage,$system_cpu_usage,$total_cpu_usage"
    echo "$nowtime,$gpucurfreq,$gpu_load,$cpu0curfreq,$cpu1curfreq,$cpu2curfreq,$cpu3curfreq,$cpu4curfreq,$cpu5curfreq,$cpu6curfreq,$cpu7curfreq,$fps,$capacity,$user_cpu_usage,$system_cpu_usage,$total_cpu_usage" >> ./$current_time/system_info_$current_time.csv
done
echo "-------------finished--------------"
